<?php

namespace app\admin\controller\xiluhouse;

use app\common\controller\Backend;
use think\Db;

/**
 * Ajax异步请求接口
 * @internal
 */
class Ajax extends Backend
{

    protected $noNeedLogin = ['lang'];
    protected $noNeedRight = ['*'];
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();

        //设置过滤方法
        $this->request->filter(['strip_tags', 'htmlspecialchars']);
    }

    /**
     * 读取地铁线，地铁站
     */
    public function subways()
    {
        $type = $this->request->get('type', 1);
        $code = $this->request->get('code');
        $where = [];
        $categorylist = null;
        if ($type == 1) {
            $categorylist = Db::name('xiluhouse_subway')->where($where)->field('id as value,name_city as name')->select();
        } else {
            $subway_info = Db::name('xiluhouse_subway')->where($where)->find($code);
            if ($subway_info) {
                $categorylist = Db::name('xiluhouse_subway_platform')->where('find_in_set(' . $subway_info['code'] . ',subways)')->field('id as value,name')->select();
            }
        }
        $this->success('', null, $categorylist);
    }

    /**
     * 读取省市区数据,联动列表
     */
    public function area()
    {
        $province = $this->request->get('prov');
        $city = $this->request->get('city');
        $area = $this->request->get('region');
        $where = ['pid' => 0, 'level' => 1];
        $provincelist = null;
        $model = new \app\common\model\xiluhouse\Area;
        if ($province !== '') {
            if ($province) {
                $where['pid'] = $model->where(['level' => 1, 'name' => ['like', $province . '%']])->value('id');
                $where['level'] = 2;
            }
            if ($city !== '') {
                if ($city) {
                    $where['pid'] = $model->where(['level' => 2, 'name' => ['like', $city . '%']])->value('id');
                    $where['level'] = 3;
                }
                $provincelist = $model->where($where)->field('name as value,name')->select();
            }
            if ($area !== '') {
                if ($area) {
                    $city_id = $model->where(['level' => 2, 'name' => ['like', $city . '%']])->value('id');
                    $where['pid'] = $model->where(['level' => 3, 'pid' => $city_id, 'name' => ['like', $area . '%']])->value('id');
                    $where['level'] = 4;
                }
                $provincelist = $model->where($where)->field('name as value,name')->select();
            }
        }
        $this->success('', null, $provincelist);
    }
    /**
     * 读取省市区数据
     */
    public function areaselectpage()
    {
        $this->model = new \app\common\model\xiluhouse\Area;
        return $this->selectpage();
    }

    //消息
    public function order_message()
    {
        //获取 待处理流失预警记录
        $data[] = \app\common\model\xiluhouse\LossWarning::where('state', 1)->count() ?: 0;
        $data[] = \app\common\model\xiluhouse\AppointLog::where('is_read', 1)->where('rental_id', 0)->count() ?: 0;
        $all_num = 0;
        foreach ($data as $key => $value) {
            $all_num += $value;
        }
        return ['data' => $data, 'all_num' => $all_num];
    }
}
